package com.example.yk_lx.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.yk_lx.R;
import com.example.yk_lx.adapter.FriendAdapter;
import com.example.yk_lx.adapter.VideoAdapter;
import com.example.yk_lx.baseMVVM.BaseFragment;
import com.example.yk_lx.databinding.FragmentMyBinding;
import com.example.yk_lx.entity.VideoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现协调者布局
 */
public class MyFragment extends BaseFragment<FragmentMyBinding,MyViewModel> {

    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;

    @Override
    protected int bindLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        videoAdapter = new VideoAdapter(R.layout.item_video,videos);
        dataBinding.rv.setAdapter(videoAdapter);
        dataBinding.rv.setLayoutManager(new GridLayoutManager(getContext(),2));

        viewModel.video();
    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                if(entity.getCode()==200){
                    videos.clear();
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }else{
                    ToastUtils.showLong(entity.getMessage());
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected Class<MyViewModel> getViewModel() {
        return MyViewModel.class;
    }
}